#include "widget.h"
#include "ui_widget.h"
#include<QPainter>
#include<QPaintEvent>
#include<qcolor.h>
#include<QRandomGenerator>
#include<QFont>
#include<QSoundEffect>
#include<QMessageBox>
#include<QPushButton>
#include<QPointF>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setGeometry(250,40,924,822);
    this->setWindowTitle("贪吃蛇");
    //使用定时器
    timer=new QTimer();
    //定时器有timeout函数
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
    //设置界面为固定大小
    resize(924,822);
    setFixedSize(924,822);
    //初始化蛇身 初始长度为3
    QRectF rect(462,411,node_Width,node_Height);
    snake.append(rect);
    addTop();
    addTop();
    //初始化奖品
    addNewReward();
}

Widget::~Widget()
{
    delete ui;
}
//按键控制
void Widget::keyPressEvent(QKeyEvent*event)
{
    switch(event->key())
        {
    case Qt::Key_Up:
        if(moveFlag!=dir_down){
        moveFlag=dir_up;
        }
        break;
    case Qt::Key_W:
        if(moveFlag!=dir_down){
        moveFlag=dir_up;
        }
        break;
    case Qt::Key_Down:
        if(moveFlag!=dir_up){
        moveFlag=dir_down;
        }
        break;
    case Qt::Key_S:
        if(moveFlag!=dir_up){
        moveFlag=dir_down;
        }
        break;
    case Qt::Key_Right:
        if(moveFlag!=dir_left){
        moveFlag=dir_right;
        }
        break;
    case Qt::Key_D:
        if(moveFlag!=dir_left){
        moveFlag=dir_right;
        }
        break;
    case Qt::Key_Left:
        if(moveFlag!=dir_right){
        moveFlag=dir_left;
        }
        break;
    case Qt::Key_A:
        if(moveFlag!=dir_right){
        moveFlag=dir_left;
        }
        break;
        //空格键控制暂停与开始
    case Qt::Key_Space:
        if(gameStart==false)
        {
            gameStart=true;
            //启动定时器
            timer->start(time);
        }
        else
        {
            gameStart=false;
            timer->stop();
        }
        break;
    default:
        break;
    }
}
void Widget::timeout(){
    //有没有重合
    int count=1;
    if(snake[0].intersects(rewardNode)){//碰到食物
        count++;
        this->grade++;
        addNewReward();
    }
    while(count--){
        switch(moveFlag)
        {
        case dir_up:
            addTop();
            //在顶部加一个小方块
            break;
        case dir_down:
            addDown();
            break;
        case dir_right:
            addRight();
            break;
        case dir_left:
            addLeft();
            break;
        default:
            break;
        }
    }
    deleteLast();
    update();
}
//通过计算得到分别向上下左右运动需要新画的方块左上右下坐标
void Widget::addTop(){
    QPointF LeftTop;
    QPointF RightBottom;
    if(snake[0].y()-node_Height<0){
        LeftTop=QPointF(snake[0].x(),this->height()-node_Height);
        RightBottom=QPointF(snake[0].x()+node_Width,this->height());
    }
    else{
        LeftTop=QPointF(snake[0].x(),snake[0].y()-node_Height);
        RightBottom=snake[0].topRight();
    }
    snake.insert(0,QRectF(LeftTop,RightBottom));
}

void Widget::addDown(){
    QPointF LeftTop;
    QPointF RightBottom;
    if(snake[0].y()+2*node_Height>this->height()){
        LeftTop=QPointF(snake[0].x(),0);
        RightBottom=QPointF(snake[0].x()+node_Width,node_Height);
    }
    else{
        LeftTop=QPointF(snake[0].x(),snake[0].y()+node_Height);
        RightBottom=QPointF(snake[0].x()+node_Width,snake[0].y()+node_Height*2);
    }
    snake.insert(0,QRectF(LeftTop,RightBottom));
}
void Widget::addLeft(){
    QPointF LeftTop;
    QPointF RightBottom;
    if(snake[0].x()-node_Width<0){
        LeftTop=QPointF(this->width()-node_Width,snake[0].y());
        RightBottom=QPointF(this->width(),snake[0].y()+node_Height);
    }
    else{
        LeftTop=QPointF(snake[0].x()-node_Width,snake[0].y());
        RightBottom=QPointF(snake[0].x(),snake[0].y()+node_Height);
    }
    snake.insert(0,QRectF(LeftTop,RightBottom));
}
void Widget::addRight(){
    QPointF LeftTop;
    QPointF RightBottom;
    if(snake[0].x()+2*node_Width>this->width()){
        LeftTop=QPointF(0,snake[0].y());
        RightBottom=QPointF(node_Width,snake[0].y()+node_Height);
    }
    else{
        LeftTop=QPointF(snake[0].x()+node_Width,snake[0].y());
        RightBottom=QPointF(snake[0].x()+2*node_Width,snake[0].y()+node_Height);
    }
    snake.insert(0,QRectF(LeftTop,RightBottom));
}

void Widget::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPen pen;
    QBrush brush;
    //背景图片
    QPixmap pix;
    pix.load(":/Pictures/Saved Pictures/sky1.jpg");
    painter.drawPixmap(0,0,924,822,pix);
    //画蛇
    pen.setColor(SnakeOutColor);
    brush.setColor(SnakeInColor);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);


    for(int i=0;i<snake.length();i++)
    {
        painter.drawRect(snake[i]);
    }
    //画奖品
    pen.setColor(Qt::darkBlue);
    brush.setColor(Qt::darkCyan);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(rewardNode);

    if(contact()==true){
        QFont font("华文行楷",100,QFont::ExtraLight,false);
        painter.setFont(font);
        QPointF center(462,500);
        QFontMetrics fm = painter.fontMetrics();
        int w = fm.horizontalAdvance(overtext);
        int h = fm.height();
        QPointF topLeft(center.x() - w / 2, center.y() - h / 2);
        painter.drawText(topLeft, overtext);
        QString text4=text3.arg(grade);
        QFont font2("华文新魏",40,Qt::black);
        painter.setFont(font2);
        painter.drawText(230,600,text4);
        timer->stop();
    }
    QFont font1("华文新魏",30,Qt::black);
    QString text2=text1.arg(grade);
    painter.setFont(font1);
    painter.drawText(0,30,text2);
}
//删除掉最后一个
void Widget::deleteLast(){
    snake.removeLast();
}
//设置奖品
void Widget::addNewReward(){
    rewardNode=QRectF(QRandomGenerator::global()->bounded(0,910),
                      QRandomGenerator::global()->bounded(0,810),
                      node_Width,
                      node_Height);
    for(int i=1;i<snake.length();i++){
        if(snake[i].intersects(rewardNode))
        {
            addNewReward();
        }
    }

}
//是否死亡
bool Widget::contact(){
    for(int i=1;i<snake.length();i++){
        if(snake[0]==snake[i])//触碰到自己
            return true;
    }
    return false;
}

































